package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.MessageplatfromDO;
import com.jinzhi.jzweb.service.MessageplatfromService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 
 * <pre>
 * 短信模板信息表
 * </pre>
 * <small> 2020-01-14 10:10:26 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/messageplatfrom")
public class MessageplatfromController extends BaseController {
	@Autowired
	private MessageplatfromService messageplatfromService;
	@Autowired
	private UserService userService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:messageplatfrom:messageplatfrom")
	String Messageplatfrom(){
	    return "jzweb/messageplatfrom/messageplatfrom";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:messageplatfrom:messageplatfrom")
	public Result<Page<MessageplatfromDO>> list(MessageplatfromDO messageplatfromDTO){
        Wrapper<MessageplatfromDO> wrapper = new EntityWrapper<MessageplatfromDO>().orderBy("id", false);
        //wrapper.eq("name","jzweb");
        Page<MessageplatfromDO> page = messageplatfromService.selectPage(getPage(MessageplatfromDO.class), wrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			MessageplatfromDO obj = (MessageplatfromDO) page.getRecords().get(i);
			if (obj.getCreator() != null) {
				UserDO userDO = userService.selectById(obj.getCreator());
				if (userDO != null) {
					obj.setCreatorName(userDO.getUsername());
				}
			}
			if (obj.getUpdater() != null) {
				UserDO userDO = userService.selectById(obj.getUpdater());
				if (userDO != null) {
					obj.setUpdaterName(userDO.getUsername());
				}
			}
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:messageplatfrom:add")
	String add(){
	    return "jzweb/messageplatfrom/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:messageplatfrom:edit")
	String edit(@PathVariable("id") Long id,Model model){
		MessageplatfromDO messageplatfrom = messageplatfromService.selectById(id);
		model.addAttribute("messageplatfrom", messageplatfrom);
	    return "jzweb/messageplatfrom/edit";
	}
	
	@Log("添加短信模板信息表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:messageplatfrom:add")
	public Result<String> save( MessageplatfromDO messageplatfrom){
		messageplatfrom.setCreator(this.getUserId());
		messageplatfrom.setCreateDate(new Date());
		messageplatfrom.setUpdater(this.getUserId());
		messageplatfrom.setUpdateDate(messageplatfrom.getCreateDate());
		messageplatfromService.insert(messageplatfrom);
        return Result.ok();
	}
	
	@Log("修改短信模板信息表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:messageplatfrom:edit")
	public Result<String>  update( MessageplatfromDO messageplatfrom){
		messageplatfrom.setUpdater(this.getUserId());
		messageplatfrom.setUpdateDate(messageplatfrom.getCreateDate());
		boolean update = messageplatfromService.updateById(messageplatfrom);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除短信模板信息表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:messageplatfrom:remove")
	public Result<String>  remove( Long id){
		messageplatfromService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除短信模板信息表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:messageplatfrom:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		messageplatfromService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
